/***************************************************************************
 *
 * File name: q4.c
 *
 * Author:  Tomer Filiba
 *          03701307-5
 *          sebulba@t2
 *
 *
 * Exercise #: 2
 * Question #: 4
 *
 * Submission Date: 11/6
 *
 **************************************************************************/

/*-------------------------------------------------------------------------
  Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>


/*=========================================================================
  Constants and definitions:
==========================================================================*/
#define GREAT_SUCCESS		(0)
#define I_WILL_BE_EXECUTE	(1)
/* 'long' type is 32 bits wide, so the we'll need to allocate at most 
    32 bytes for the buffer */
#define MAX_DIGITS			(32)


/*-------------------------------------------------------------------------
  The main program: reads a source number, source base and target base,
  and converts the number from the source base to the target base
 -------------------------------------------------------------------------*/
int main4(int argc, const char ** argv)
{
	int srcbase, dstbase, i;
	char srcnum[MAX_DIGITS+1]; /* +1 for \0 */
	char *ch, c;
	unsigned long decimal; 
	char result[MAX_DIGITS];
	
	/* get input from user */
	printf("Source base: ");
	scanf("%d", &srcbase);
	printf("Source number: ");
	scanf("%32s", &srcnum);
	printf("Target base: ");
	scanf("%d", &dstbase);
	
	/* validate ranges */
	if (srcbase < 2 || srcbase > 10) {
		printf("Error! Source base must be in range 2..10\n");
		return I_WILL_BE_EXECUTE;
	}
	if (dstbase < 2 || dstbase > 10) {
		printf("Error! Target base must be in range 2..10\n");
		return I_WILL_BE_EXECUTE;
	}
	
	/* convert given number (as a string) to a decimal number */
	decimal = 0;
	for (ch = srcnum; (c = *ch) != '\0' ; ch++)
	{
		if (c < '0' || c >= '0' + srcbase) {
			printf("Error! Invalid character '%c' in number of base %d\n", 
				c, srcbase);
			return I_WILL_BE_EXECUTE;
		}
		decimal *= srcbase;
		decimal += c - '0'; /* the value of the digit */
	}
	
	/* handle the trivial case of 0 */
	if (decimal == 0) {
		printf("0\n");
		return GREAT_SUCCESS;
	}
	
	/* convert decimal number to target base */
	printf("Target number: ");
	for (i = 0; decimal != 0; i++) {
		if (i >= MAX_DIGITS) {
			/* this should never actually happen, but still we check, 
			 * to be on the safe side */
			printf("Error! Not enough digits to convert number\n");
			return I_WILL_BE_EXECUTE;
		}
		result[i] = (char)(decimal % dstbase);
		decimal /= dstbase;
	}
	
	/* print the resulting digits, in reverse order */
	for(i--; i >= 0; i--) {
		printf("%d", result[i]);
	}
	printf("\n");
	
	return GREAT_SUCCESS;
}



/**************************************************************************
			                          End of file
***************************************************************************/